//
//  LCUtils.m
//  JKBiblicalReading
//
//  Created by admin on 2018/8/16.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCUtils.h"
#import "LSZNewAudioHomepageManager.h"//lcListPlay

@implementation LCUtils

#define kMaxLength 500

- (void)textFiledEditChangedWithTextField:(UITextView *)textField{
    //检测文本改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(putInTextFieldEditChanged:) name:UITextViewTextDidChangeNotification object:textField];
    self.showNumTextLabel.text = [NSString stringWithFormat:@"%d", kMaxLength];
}

- (void)putInTextFieldEditChanged:(NSNotification *)obj {
    UITextView *textField = (UITextView *)obj.object;
    NSString *toBeString = textField.text;
#pragma mark -下午8:17 字数限制 -
    NSInteger index = kMaxLength - toBeString.length;
    // 键盘输入模式
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage;
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > kMaxLength) {
                textField.text = [toBeString substringToIndex:kMaxLength];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > kMaxLength) {
            textField.text = [toBeString substringToIndex:kMaxLength];
        }
    }
#pragma mark - 下午8:16 字数限制 -
    if (index < 0) {
        index = 0;
    }
    self.showNumTextLabel.text = [NSString stringWithFormat:@"%ld", (long)index];
//    NSLog(@"self.showNumTextLabel.text = %@", self.showNumTextLabel.text);
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
///检测文本改变
- (void)textFiledComposeEditChanged{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewTextDidChange) name:UITextViewTextDidChangeNotification object:nil];
}
- (void)textViewTextDidChange {
    if (self.composeEditBlock != nil) {
        self.composeEditBlock();
    }
}
///根据shengjingTitle和笔记数组处理Tag之后返回Tag值 第几行加上id
+ (NSString *)getTagWithNotesContentArr:(NSArray *)notesContentArr shengJingTitle:(NSString *)shengJingTitle {
    NSString *tagRightStr = [[NSString stringWithFormat:@"%@***",notesContentArr.lastObject] substringWithinBoundsLeft:@" " right:@"***"];
    NSString *tagLeftStr = [[NSString stringWithFormat:@"%@",notesContentArr.lastObject] substringWithinBoundsLeft:@":" right:@" "];
    NSLog(@"tagLeftStr = %@", tagLeftStr);
    NSString *tag = [NSString stringWithFormat:@"%@:%@",tagLeftStr,[_TheHomeM getJieIDWithSelectContent:tagRightStr shengJingTitle:shengJingTitle]];
    return tag;
}

#pragma mark - 时间戳1970不除以1000
+(NSString *)lcGetThePreviousTimeWithGetTime:(NSString *)getTime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[getTime doubleValue]];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

#pragma mark - 判断有没有网络
+ (Boolean)checkReachability {
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://baidu.com"];
    if ([reach currentReachabilityStatus] == NotReachable){
        return false;
    }else{
        return true;
    }
}

///自定义窗的 背景是白色，字是黑色 无图片
+ (void)showHud2:(NSString *)str{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
    [SVProgressHUD setInfoImage:nil];
#pragma clang diagnostic pop
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom]; //自定义窗的 背景是白色，字是黑色
    [SVProgressHUD setForegroundColor:[UIColor blackColor]];
    [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1.00]];
    [SVProgressHUD setFont:[UIFont systemFontOfSize:13]];
    [SVProgressHUD showInfoWithStatus:str];
    [SVProgressHUD dismissWithDelay:1];
}

+ (void)lcListPlayWithLSZNewAudioHomepageManager:(LSZNewAudioHomepageManager *)manager {
    LSZNewAudioHomepageMg *mgPlay = manager.arrayData.firstObject;
    NSDictionary *dictionary = [mgPlay yy_modelToJSONObject];
    [LSZSpecialPackaging addPlaylistWithArrayData:@[dictionary]];
    LCPlaylist *playlistTest = _PlaylistMgr.allPlaylists.firstObject;
    _LCPlayer.songs = playlistTest.allSongs;
    if (_LCPlayer.songs != nil) {
        [_LCPlayer playAtIndex:0 isSelectPlay:YES];
    }
}
+(void)albumArtistDetailsInterfaceJump:(LSZNewAudioPlayImageAndTableViewVC *)vc audioMg:(LSZNewAudioHomepageMg *)audioMg typeComeWhere:(NSString *)typeComeWhere{
    vc.strTopImageView = audioMg.image;
    vc.strLastImageView = audioMg.image;
    vc.type = audioMg.type;
    vc.typeStr = audioMg.type;
    vc.parentId = audioMg.id_id;
    vc.contentStr = audioMg.content;
    audioMg.originalHits = [NSString stringWithFormat:@"%ld",(long)[audioMg.originalHits integerValue]+1];
    audioMg.hits = [LSZSpecialPackaging theNumberViewsForTheAlbumAhanges:audioMg.originalHits];
    vc.total_song = audioMg.hits;
    vc.likes = [audioMg.likes integerValue];
    if([audioMg.type isEqualToString:@"geshou"]){
        vc.titleStr = @"";//专辑的名字
        vc.author = audioMg.singerName;//作者的名字
    }else{
        vc.titleStr = audioMg.album_name;//专辑的名字
        vc.author = audioMg.artist_name;//作者的名字
    }
    vc.typeComeWhere = typeComeWhere;
}
//音频更多界面跳到专辑和歌曲的
+(void)moveAlbumArtistDetailsInterfaceJump:(LSZNewAudioPlayImageAndTableViewVC *)vc audioMg:(LSZAudioMg *)audioMg typeComeWhere:(NSString *)typeComeWhere
{
    vc.strTopImageView = audioMg.image;
    vc.strLastImageView = audioMg.image;
    vc.type = audioMg.type;
    vc.typeStr = audioMg.type;
    vc.parentId = audioMg.id_id;
    vc.contentStr = audioMg.content;
    audioMg.originalHits = [NSString stringWithFormat:@"%ld",(long)[audioMg.originalHits integerValue]+1];
    audioMg.hits = [LSZSpecialPackaging theNumberViewsForTheAlbumAhanges:audioMg.originalHits];
    vc.total_song = audioMg.hits;
    vc.likes = [audioMg.likes integerValue];
    if([audioMg.type isEqualToString:@"geshou"]){
        vc.titleStr = @"";//专辑的名字
        vc.author = audioMg.singerName;//作者的名字
    }else{
        vc.titleStr = audioMg.album_name;//专辑的名字
        vc.author = audioMg.artist_name;//作者的名字
    }
    vc.typeComeWhere = typeComeWhere;
}
+(void)collectionAlbumArtistDetailsInterfaceJump:(LSZNewAudioPlayImageAndTableViewVC *)vc audioMg:(LSZSongCollectionListMg*)audioMg typeComeWhere:(NSString *)typeComeWhere
{
    vc.strTopImageView = audioMg.image;
    vc.strLastImageView = audioMg.image;
    vc.type = audioMg.type_type;
    vc.typeStr = audioMg.type_type;
    vc.parentId = audioMg.id_id;
    vc.contentStr = audioMg.content;
    audioMg.originalHits = [NSString stringWithFormat:@"%ld",(long)[audioMg.originalHits integerValue]+1];
    audioMg.hits = [LSZSpecialPackaging theNumberViewsForTheAlbumAhanges:audioMg.originalHits];
    vc.total_song = audioMg.hits;
    vc.likes = [audioMg.likes integerValue];
    if([audioMg.type isEqualToString:@"geshou"]){
        vc.titleStr = @"";//专辑的名字
        vc.author = audioMg.singerName;//作者的名字
    }else{
        vc.titleStr = audioMg.title;//专辑的名字
        vc.author = audioMg.singerName;//作者的名字
    }
    vc.typeComeWhere = typeComeWhere;
}
/**增加侧滑返回,无其他作用*/
+ (void)lcAddSideLeftView:(UIView *)view {
    UIView *sideLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, ScreenHeight)];
    [view addSubview:sideLeftView];
    [view bringSubviewToFront:sideLeftView];
}


///封装的文章登录提示
+ (void)setupLoginPromptWithAddSubview:(UIView *)view login:(void (^)(UILabel *loginLabel,UIView *loginView))loginBlock
{
    UIView *loginView = [[UIView alloc] init];
    loginView.backgroundColor = [UIColor colorWithRed:0.89 green:0.32 blue:0.30 alpha:1.00];//登录提示bg
    [view addSubview:loginView];
    UILabel *loginLabel = [[UILabel alloc] init];
    loginLabel.text = @"去登录";
    loginLabel.layer.masksToBounds = YES;
    // 设置圆角大小
    loginLabel.layer.cornerRadius = 12.0 ;
    loginLabel.textColor = [UIColor colorWithRed:0.89 green:0.32 blue:0.30 alpha:1.00];
    loginLabel.textAlignment = NSTextAlignmentCenter;
    loginLabel.backgroundColor = [UIColor whiteColor];
    [loginView addSubview:loginLabel];
    loginLabel.userInteractionEnabled = YES;
    
    UILabel *tishiLabel = [[UILabel alloc] init];
    tishiLabel.text = @"登录领取福音值";
    tishiLabel.textColor = [UIColor whiteColor];
    [loginView addSubview:tishiLabel];
    [tishiLabel sizeToFit];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor clearColor];
    [loginView addSubview:lineView];
    if (@available(iOS 11, *)) {
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(0);
            make.right.equalTo(view.mas_right).offset(0);
            make.bottom.equalTo(view.mas_safeAreaLayoutGuideBottom).offset(-50);
            make.height.mas_equalTo(1);
        }];
    }else{
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(0);
            make.right.equalTo(view.mas_right).offset(0);
            make.bottom.equalTo(view.mas_bottom).offset(-50);
            make.height.mas_equalTo(1);
        }];
    }
    [loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineView.mas_left).offset(0);
        make.right.equalTo(lineView.mas_right).offset(0);
        make.bottom.equalTo(lineView.mas_bottom).offset(0);
        make.height.mas_equalTo(50);
    }];
    //    if(![[_UserM usercode] isEqualToString:@""]){
    //        loginLabel.hidden = YES;
    //        tishiLabel.hidden = YES;
    //        loginView.hidden = YES;
    //    }
    if (loginBlock) {
        loginBlock(loginLabel,loginView);
    }
    [loginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineView.mas_right).offset(-20);
        make.top.equalTo(loginView.mas_top).offset(10);
        make.height.mas_equalTo(@28);
        make.width.mas_equalTo(@68);
    }];
    [tishiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineView.mas_left).offset(20);
        make.centerY.equalTo(loginLabel.mas_centerY);
        make.height.equalTo(loginLabel.mas_height);
        make.width.mas_equalTo(tishiLabel.mas_width);
    }];
}
///MARK:- 系统更多分享
+ (void)openOSMoreWithWebLink:(NSString *)webLink title:(NSString *)title text_image:(UIImage *)text_image{
    
    // 1、设置分享的内容，并将内容添加到数组中
    NSMutableArray *mutableArray = [NSMutableArray array];
    if (title != nil) {
        [mutableArray addObject:title];
    }
    UIImage *shareImage = text_image;
    if (shareImage) {
        [mutableArray addObject:shareImage];
    }
    NSURL *shareUrl = [NSURL URLWithString:webLink];
    if (webLink) {
        [mutableArray addObject:shareUrl];
    }
    
    // 2、初始化控制器，添加分享内容至控制器
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:mutableArray.copy applicationActivities:nil];
    //屏蔽掉系统的打印,拷贝,发给指定联系人,保存图片
    activityVC.excludedActivityTypes = @[UIActivityTypeMail,UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
    activityVC.modalInPopover = YES;
    // 3、设置回调
    UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
        
        if (completed == YES) {
            NSLog(@"分享成功");
        }else{
            NSLog(@"cancel");
        }
    };
    activityVC.completionWithItemsHandler = itemsBlock;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:activityVC animated:YES completion:nil];
}
#pragma mark-shotBlock
///截屏分享
+ (void)shotBlock:(UIImage *)screenshot
          success:(void (^)(NSInteger type))successBlock
           hidden:(void (^)(void))hiddenBlock
{
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:@"" cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"分享微信",@"分享朋友圈",@"分享动态",@"更多"] actionSheetBlock:^(NSInteger buttonIndex) {
        if (successBlock) {
            successBlock(buttonIndex);
        }
    }];
    [actionSheet show];
}
/// 自适应图片高度
+ (CGFloat)picValueToFitValueForNum:(CGFloat)num {
    num = num * ScreenWidth / 375.0f;
    return num;
}
///改变UIButton文字的对其方式
+ (void)didChangeButtonLeftForAlignment:(UIButton *)button {
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
}

@end
